package com.manong.led.controller;


import com.google.gson.Gson;
import com.manong.led.entity.Led;
import com.manong.led.service.LedService;
import com.manong.mqtt.IMqttSender;
import com.manong.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/led")
public class LedController {
    @Resource
    Gson gson;
    /**
     * 注入发送MQTT的Bean
     */
    @Resource
    private IMqttSender iMqttSender;
    @Resource
    private LedService ledService;
    /**
     * 添加灯光控制
     */
    @PostMapping("/add")
    public Result add(@RequestBody Led led){
        String conjson=gson.toJson(led);
        iMqttSender.sendToMqtt("testbb/"+led.getDeviceId()+"/controller", conjson);
        if (ledService.save(led)){
            return Result.ok().message("灯光添加成功");
        }
        return Result.error().message("灯光添加失败");
    }
}

